<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      #template {
        display: none;
      }
    </style>
  </head>
  <body>
    <div id="template">
      <!-- @fallback="fallback" -->
      <div class="container">
        <test />
      </div>
    </div>
    <div id="app"></div>
    <script src="../../../../packages/vue/dist/vue.global.js"></script>

    <script>
      const { createApp, ref, defineAsyncComponent } = Vue

      const TodoListPromise = new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve({
            setup() {
              const count = ref(1)
              const add = () => count.value++
              return {
                count,
                add
              }
            },
            template: `
                    <ul>
                        <li @click="add">{{count}}</li>
                        <li>2</li>
                        <li>3</li>
                    </ul>
                  `,
            //! 父元素是suspense
            mounted() {}
          })
        }, 2000)
      })

      const template = document.querySelector('#template')?.innerHTML
      const App = {
        setup() {
          const fallback = () => {
            console.log('fallback')
          }

          const pending = () => {
            console.log('pending')
          }

          const resolve = () => {
            console.log('resolve')
          }

          const msg = ref('message----')
          return {
            msg,
            fallback,
            resolve,
            pending
          }
        },
        components: {
          Test: {
            setup() {
              const count = ref(0)
              const add = () => count.value++

              return {
                count,
                add
              }
            },
            template: `
              <suspense
                @pending="pending"
                @fallback="() => { console.log('msg:', msg) }"
              >
                <template v-slot:default>
                  <div>
                    <todo-list />
                    <h1 @click="add">{{count}}</h1>
                  </div>
                </template>
                <template v-slot:fallback>
                  <div v-once>
                    <h1 >loading...</h1>
                    <h1 @click="add">{{count}} -loading</h1>
                  </div>
                </template>
              </suspense>
            `,
            directives: {
              xx: {
                onMounted() {
                  console.log('----')
                }
              }
            },
            components: {
              TodoList: defineAsyncComponent(() => TodoListPromise)
            }
          }
        },
        template
      }

      createApp(App).mount('#app')
    </script>
  </body>
</html>
